package org.whh.auth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.whh.auth.WhhPermission;
import org.whh.auth.domain.Permission;
import org.whh.auth.mapper.PermissionMapper;
import org.whh.auth.service.IPermissionScanService;
import org.whh.base.util.ClassUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@Service
public class PermissionScanServiceImpl implements IPermissionScanService {
    @Value("${permission.scan-package}")
    String scanPackage;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    @Transactional
    public void scan() {
        List<Class> clazzs = ClassUtils.getAllClassName(this.scanPackage);
        List<Permission> permissions = new ArrayList<>();
        if(clazzs != null && clazzs.size() > 0){
            for (int i = 0; i < clazzs.size(); i++) {
                WhhPermission annotation = (WhhPermission)clazzs.get(i).getAnnotation(WhhPermission.class);
                if(annotation==null){
                    continue;
                }
                RequestMapping annotation1 = (RequestMapping)clazzs.get(i).getAnnotation(RequestMapping.class);
                String ClassUrl = annotation1.value()[0];
                Permission permission=new Permission();
                permission.setName(annotation.name());
                permission.setUrl(ClassUrl);
                permission.setDescs(annotation.desc());
                permission.setSn(clazzs.get(i).getSimpleName());
                permissions.add(permission);

                System.out.println(permission.getName());
                Method[] methods = clazzs.get(i).getMethods();

                for (Method method : methods) {
                    WhhPermission methodAnnotation = method.getAnnotation(WhhPermission.class);
                    if(methodAnnotation==null){
                        continue;
                    }
                    Permission permission1=new Permission();
                    permission1.setName(methodAnnotation.name());
                    permission1.setDescs(methodAnnotation.desc());
                    // 处理sn  类名:方法名
                    String sn = clazzs.get(i).getSimpleName()+":"+method.getName();
                    permission1.setSn(sn);
                    // 处理url  url指的是前端在访问时需要访问的url,所以它是类上面的@RequestMapping的值+方法上面的那个url
                    String methodUrl = getMethodUrl(method);
                    permission1.setUrl(ClassUrl+methodUrl);
                    permission1.setParent(permission);
                    System.out.println("shang");
                    System.out.println(permission1);// 以类上面解析的permission对象作为父权限对象
                    System.out.println("==================");

                    permissions.add(permission1);
                }

            }
        }
        permissionMapper.removeAll();
        for (Permission permission : permissions) {
            System.out.println("前"+permission);
            permissionMapper.insert(permission);
            System.out.println("hou"+permission);
        }
    }

    private String getMethodUrl(Method method) {
        String methodUrl = "";
        if(method.getAnnotation(PutMapping.class)!=null){
            methodUrl =method.getAnnotation(PutMapping.class).value().length>0&&method.getAnnotation(PutMapping.class).value()!=null? method.getAnnotation(PutMapping.class).value()[0]:"";
        }
        if(method.getAnnotation(PostMapping.class)!=null){
            methodUrl =method.getAnnotation(PostMapping.class).value().length>0&&method.getAnnotation(PostMapping.class).value()!=null? method.getAnnotation(PostMapping.class).value()[0]:"";
        }
        if(method.getAnnotation(PutMapping.class)!=null){
            methodUrl =method.getAnnotation(PutMapping.class).value().length>0&&method.getAnnotation(PutMapping.class).value()!=null? method.getAnnotation(PutMapping.class).value()[0]:"";
        }
        if(method.getAnnotation(GetMapping.class)!=null){
            methodUrl =method.getAnnotation(GetMapping.class).value().length>0&&method.getAnnotation(GetMapping.class).value()!=null? method.getAnnotation(GetMapping.class).value()[0]:"";
        }
        if(method.getAnnotation(DeleteMapping.class)!=null){
            methodUrl =method.getAnnotation(DeleteMapping.class).value().length>0&&method.getAnnotation(DeleteMapping.class).value()!=null? method.getAnnotation(DeleteMapping.class).value()[0]:"";
        }
        if(method.getAnnotation(PatchMapping.class)!=null){
            methodUrl =method.getAnnotation(PatchMapping.class).value().length>0&&method.getAnnotation(PatchMapping.class).value()!=null? method.getAnnotation(PatchMapping.class).value()[0]:"";
        }

        return methodUrl;
    }
}
